package com.googlecode.ddlutilstool.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.apache.log4j.lf5.viewer.LF5SwingUtils;

import com.googlecode.ddlutilstool.logging.Log4JWriterObserable;
import com.googlecode.ddlutilstool.logging.Log4jWriterObserver;

/**
 * <p>Class LogPanel</p>
 * 
 * <p>This panel contains the output from Log4J.  The logging
 * will show up and the window scroller will keep it scrolled to
 * the bottom.</p>
 */
@SuppressWarnings("serial")
public class LogPanel extends JPanel implements Log4jWriterObserver {

	private final static int DEFAULT_WINDOW_WIDTH = 900;
	private final static int DEFAULT_WINDOW_HEIGHT = 131;
	
	private JTextArea textArea = null;
	private JScrollPane pane = null;
	
	public LogPanel() {
		super();
		Log4JWriterObserable.addObserver(this);
		setLayout(new BorderLayout());
		textArea = new JTextArea();
		textArea.setEditable(false);
	    pane = new JScrollPane(textArea);
	    add(pane);
	    LF5SwingUtils.makeVerticalScrollBarTrack(pane);
	    setPreferredSize(new Dimension(DEFAULT_WINDOW_WIDTH,DEFAULT_WINDOW_HEIGHT));
	}
	
	/**
	 * Clears the display.
	 */
	public void clean() {
		textArea.setText("");
	}

	/**
	 * Called by the Log4JWriterObersable Appender with
	 * a new log output, it appends the contents to the
	 * window.
	 */
	public void append(String pLine) {
		textArea.append(pLine);
		
		validate();
	}
	
	
}
